using System.Collections;
using UnityEngine;
/// <summary>
/// 闪避状态
/// </summary>
public class DodgeState : IState
{
    public DodgeState(FSM manager)
    {
        this.manager = manager;
        this.parameter = manager.parameter;
    }

    public override void OnEnter()
    {
        // parameter.animator.Play("Dodge");
        
    }
    public override void OnUpdate()
    {
        parameter.animator.SetBool("isDodging", parameter.isDodging);
        //受击
        if (parameter.isHurt)
        {
            manager.TransitionState(StateType.Hit);
        }
        //动画播放95%切换到待机状态
        // if (parameter.animatorStateInfo.normalizedTime >= .95f)
        // {
        //     manager.TransitionState(StateType.Idle);
        // }
        if (parameter.isDodging == false)
        {
            manager.TransitionState(StateType.Idle);
        }
    }

    public override void OnFixedUpdate()
    {
        manager.Move();
        Dodge();
    }

    public override void OnExit() {
        AudioManager.Instance.PlaySFX("闪避");
        parameter.isDodging = false;
    }

    // 进行闪避操作的方法
    public void Dodge()
    {
        //冷却结束
        if (!parameter.isDodgeOnCooldown)
        {
            if (parameter.dodgeTimer <= parameter.dodgeDuration)
            {
                // 施加闪避力量，根据输入方向和设定的闪避力量
                parameter.rb.AddForce(parameter.inputDirection * parameter.dodgeForce, ForceMode2D.Impulse);

                parameter.dodgeTimer += Time.fixedDeltaTime;
            }
            else
            {
                parameter.isDodging = false;
                parameter.isDodgeOnCooldown = true;
                manager.DodgeOnCooldown();// 开始闪避冷却
                parameter.dodgeTimer = 0f;
                //更新闪避cd UI
                PlayerEvents.UpdateDodgeCD(parameter.dodgeCooldown);
            }
        }
    }
}
